<?php

namespace App\Http\Controllers;

use App\Exceptions\StringHelper;
use App\Jobs\AppointList;
use App\Models\Appoint;
use App\Models\AppointQueue;
use App\Models\HospitalAppointType;
use App\Models\Interview;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OldAppointController extends Controller
{
    public $hs = [
        '9RV7H6Dv' => 248035,
        'tU459foO' => 248033,
        'a94PW3iX' => 206262,
        'KPW01H7g' => 113890,
        'E1mUGz95' => 192821,
        'ER8GOz85'=>257888,
        'y479PewU'=>314896,
        'YPYGI9lE'  =>590848,
        '91e43c3'=>89154,
        'b78e2b6'=>925348,
        'c04d4156'=>1511688,
        'ds843j32'=>1550005,
        'ebb7df6'=>206260,
        '4kl29k1'=>1582088,
        '121212'=>184793,
        '121dsd'=>176156
    ];
    public static $typeText = [
        1 => 1,
        2 => 2,
        3 => 8,
        13 => 4,
        4 => 5,
        5 => 6,
        6 => 3,
        7 => 7,
        8 => 9,
        9  =>10,
        10  =>11,
        11 =>12,
    ];

    public static $typeTexts = [
        1 => '儿童体检',
        2 => '儿童疫苗',
        8 => '入托体检',
        4 => '成人疫苗',
        5 => '建册预约',
        6 => '领取叶酸预约',
        3 => '微量元素',
        7 => '两癌筛查',
        9 => '核酸检测',
        10 => '盆底功能筛查及治疗',
        11 => '冬病夏治三伏贴',
        12 => '儿童推拿门诊',
        13 => '专病预约',
    ];


    public function sign($h, $d, $s,$q='')
    {
        //echo md5($h.$q.date('Ymd')."rh6FcKyWOUqF52hf");exit;
        if ($s == md5($h .$q. date('Ymd') . "rh6FcKyWOUqF52hf")) {
            return true;
        }
        return false;
    }
    public function list(Request $request)
    {
        $h = $request->input('h');
        $d = $request->input('d');
        $s = $request->input('s');

        $arr = [];

        $types=[
            1=>2,
            2=>1,
            4=>3,
        ];
        if ($this->sign($h, $d, $s) && $this->hs[$h]) {

            $appoint = Appoint::query()->whereIn('hospital_id', $this->hs[$h])
                ->where('appoint_date' , date('Y-m-d'))
                ->get();
            foreach ($appoint as $k => $v) {
                $rs['pat_Id'] = "appoint:" . $v->id;
                $family = $v->family;
                $rs['pat_Name'] = isset($family)?$v->family->name:"";

                if(isset($family)) {
                    $DiffDate = StringHelper::DiffDate(date('Y-m-d', time()), $v->family->birthday);
                    if ($DiffDate[0]) {
                        $age = $DiffDate[0] . "岁";
                    } elseif ($DiffDate[1]) {
                        $age = $DiffDate[1] . "月";
                    } else {
                        $age = $DiffDate[2] . "天";
                    }
                }else{
                    $age = "";
                }
                $rs['age'] = $age;

                $rs['yuyueRiqi'] = date('Y-m-d');


                $index = Appoint::query()
                    ->where('appoint_date' , $v->appoint_date)
                    ->where( 'id','<', $v->id)
                    ->where('hospital_id' , $v->doctorid)
                    ->where('appoint_time' , $v->appoint_time)
                    ->where('type' , $v->type)
                    ->count();
                $n = $index + 1;
                $rs['seq_No'] = $n;

                switch ($v->doctorid) {
                    case 248033:
                        $quyuName = 1;
                        break;
                    case 234820:
                        $quyuName = 2;
                        break;
                    default:
                        $quyuName = 0;
                        break;
                }

                if(!$type=$types[$v->type]){
                    $type=$v->type;
                }

                $rs['dept_Name'] = $type;
                $interval = $v->interval;
                $rs['yuyueDate'] = isset($interval)?$interval->start."-".$interval->end:"";
                $rs['quyuName'] = $quyuName;
                $arr[] = $rs;
            }
        } else {
            $code = 20010;
            $msg = 'sign错误';
        }
        return new JsonResponse(['code' =>  isset($code)?$code:10000, 'msg' => isset($msg)?$msg:'成功', 'data' => $arr],200);
    }

    public function data(Request $request)
    {
        $arr = [];
        $h = $request->input('h');
        $d = $request->input('d');
        $s = $request->input('s');


        if ($this->sign($h, $d, $s) && $this->hs[$h]) {
            $appoint = Appoint::query()
                ->get();
            foreach ($appoint as $k => $v) {
                $rs['qrcode'] = "appoint:" . $v->id;

                $family = $v->family;
                $rs['name'] = isset($family)?$v->family->name:"";

                $rs['date'] = $v->appoint_date;
                $rs['project'] = self::$typeText[$v->type];

                $interval = $v->interval;
                $rs['time'] = isset($interval)?$interval->start."-".$interval->end:"";
                $time = explode('-',$rs['time']);
                $rs['start'] = date('Y-m-d '.$time[0].':00');
                $rs['end'] = date('Y-m-d '.$time[1].':00');
                $rs['appointid'] = $v->id;
                $rs['userid'] = $v->user_id;
                $rs['project_name'] = $v->type == 2 || $v->type == 13?$v->vaccine->name:$v->appointType->name;
                $arr[] = $rs;

            }
        } else {
            $code = 20010;
            $msg = 'sign错误';
        }
        return new JsonResponse(['code' =>  isset($code)?$code:10000, 'msg' => isset($msg)?$msg:'成功', 'data' => $arr,'project_t'=>self::$typeTexts],200);

    }

    public function actionDoneNew(Request $request)
    {
        $h = $request->input('h');
        $d = $request->input('d');
        $s = $request->input('s');
        $q = $request->input('q');
        if ($this->sign($h, $d, $s,$q) && $this->hs[$h]) {

            $appid=explode(':',$q);
            if($id=intval($appid[1])){
                $appoint=Appoint::query()->where('id',$id)->first();
                if($appoint){
                    $appoint->state=2;
                    $appoint->save();
                }else{
                    $code = 20010;
                    $msg = '参数错误';
                }
            }else{
                $code = 20010;
                $msg = '参数错误';
            }

        } else {
            $code = 20010;
            $msg = 'sign错误';
        }
        return new JsonResponse(['code' =>  isset($code)?$code:10000, 'msg' => isset($msg)?$msg:'成功', ],200);
    }
    public function actionDel($doctorid){
        AppointCallingList::deleteAll(['doctorid' => $doctorid]);
        $redis=\Yii::$app->rd;
        $redis->del('Queue-'.$doctorid.'-'.date('Y-m-d').'-2-3');
        $redis->del('Queue-'.$doctorid.'-'.date('Y-m-d').'-2-');
        $redis->del('Queue-'.$doctorid.'-'.date('Y-m-d').'-2-s');

        $redis->del('Queue-'.$doctorid.'-'.date('Y-m-d').'-2-3s');

        $redis->del('Queue-'.$doctorid.'-'.date('Y-m-d').'-1-0');
        $redis->del('Queue-'.$doctorid.'-'.date('Y-m-d').'-4-0');

    }

    public function actionDone(Request $request){
        $h = $request->input('h');
        $d = $request->input('d');
        $s = $request->input('s');
        $q = $request->input('q');
        if ($this->sign($h, $d, $s) && $this->hs[$h]) {
            return ['code' => 10000, 'msg' => '成功','data'=>[]];
        }
    }

    public function Calling(Request $request){
        $h = $request->input('h');
        $aString = $request->input('aString');

        $appointArray=explode(':',$aString);
        $id=$appointArray[1];
        if($id == 'test'){
            return  new JsonResponse(['code' => 10000, 'msg' => '成功',
                'data' => [
                    'name' => '测试',
                    'type' => '测试',
                    'hospital' => '测试',
                    'num' => '测试',
                    'deng' => '测试',
                    'date' => date('Y年m月d日') . "测试",
                ]]);
        }
        $doctorid=$this->hs[$h];
        $t=date('H:i');
        if($t>'17:00' || $t<'07:00'){
            return  new JsonResponse(['code' => 20000, 'msg' => '可使用时间为:<br>早7点至下午5点']);
        }
        $appoint = Appoint::query()
            ->where('status',1)
            ->where('type',2)
            ->where('hospital_id',$doctorid)
            ->where('id',$id)
            ->where('appoint_date',date('Y-m-d'))
            ->first();
        if(!$appoint){
            return  new JsonResponse(['code'=>30000,'msg'=>'未查询到预约信息']);
        }
        $appointQueue = AppointQueue::query()->where('appoint_id',$id)->first();
        if($appointQueue){
            if($appointQueue->status == 3){
                $msg = "预约已完成";
            }else{
                $msg = "已排队";
            }
            return  new JsonResponse(['code' => 20000, 'msg' => $msg]);
        }
        $appointQueue = new AppointQueue();
        $appointQueue->name = $appoint->family->name;
        $appointQueue->interval_id = $appoint->appoint_time;
        $appointQueue->type = $appoint->type;
        $appointQueue->hospital_id = $appoint->hospital_id;
        $appointQueue->appoint_id = $appoint->id;
        $appointQueue->appoint_date = $appoint->appoint_date;

        if($appointQueue->save()){
            $deng = AppointQueue::query()
                ->where('appoint_date',$appoint->appoint_date)
                ->where('hospital_id',$appoint->hospital_id)
                ->where('type',$appoint->type)
                ->where('num','<',$appointQueue->num)
                ->where('status',1)
                ->count();

            return new JsonResponse(['code' => 10000, 'msg' => '成功',
                'data' => [
                    'name' => $appoint->family->name,
                    'type' => $appoint->appointType->name,
                    'hospital' => $appoint->hospital->name,
                    'num' => $appointQueue->num,
                    'deng' => $deng,
                    'date' => date('Y年m月d日') . " " . $appoint->interval->timeText()
                    ]
                ]);
        }
    }
}
